package plannegocio.web;

import java.io.FileInputStream;
import java.io.InputStream;
//import java.nio.file.Files;
import java.util.Collection;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.io.IOUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import plannegocio.modelo.Persona;
import plannegocio.modelo.PlanNegocio;
import plannegocio.negocio.GestionPlannegocios;


@Controller
public class PlanNegocioxSabioController {
	
	@RequestMapping(value = "/ListarPlanesxSabio")	
	protected ModelAndView ListarPlanes_xSabio(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		
		System.out.println("planesx sabio");	
		HttpSession sesion=request.getSession(true);
		Persona bean = (Persona) sesion.getAttribute("personalogueada");
		int idsabio=bean.getPersonaID();
		
		GestionPlannegocios negocio = new GestionPlannegocios();
		Collection<PlanNegocio> listaplanesxsabio = null;
		listaplanesxsabio = negocio.listarxSabio(idsabio);
		return new ModelAndView("planesxsabio", "PLANESXSABIO", listaplanesxsabio);
	}
	/**
	download
	     */	
	@RequestMapping(value = "/download")
    protected ModelAndView download(HttpServletRequest request,
        HttpServletResponse response) throws Exception {
		
		int id = ServletRequestUtils.getRequiredIntParameter(request, "id");
		String nombreFichero = "archivo"+ Integer.toString(id) +".pdf";
        String unPath = "d:/";

        response.setContentType("application/pdf");
        response.setHeader("Content-Disposition", "attachment; filename=\""
                + nombreFichero+ "\"");

        InputStream is = new FileInputStream(unPath+nombreFichero);
        
        IOUtils.copy(is, response.getOutputStream());
        
        response.flushBuffer();
		/*/***********************************
    	int id = ServletRequestUtils.getRequiredIntParameter(request, "id");
    	PlanNegocio plan = new PlanNegocio();
    	GestionPlannegocios negocio = new GestionPlannegocios();
    	plan=negocio.obtener(id);
    	Byte oFile = plan.getDocumento_adjunto();
 
    	response.setContentType( "application/octet-stream" );

        response.setHeader("Content-Disposition","attachment; filename=\"" + "abc.txt" + "\";");
        response.setHeader("cache-control", "no-cache");
        response.setHeader("cache-control", "must-revalidate");

        ServletOutputStream outs = response.getOutputStream();
        outs.write(oFile);
        outs.flush();
        outs.close();***********************/
        return null;
    	
        //response.setHeader("Content-Disposition","attachment; filename=\"" + oFile.getFilename() +"\"");
        //FileCopyUtils.copy(file.getFile(), response.getOutputStream());
        //return null;
    }

}
